package com.yunpeng.dishes.biz.services.impl;

import com.yunpeng.dishes.biz.services.IDishesInternalService;
import com.yunpeng.dishes.dao.mapper.entity.IDishesMapper;
import com.yunpeng.dishes.dao.mapper.entity.IMerchantDishesMapper;
import com.yunpeng.dishes.dao.mapper.relation.IDishesFoodIngredientsMapper;
import com.yunpeng.dishes.dao.mapper.relation.IDishesNutrientsMapper;
import com.yunpeng.dishes.dao.model.entity.DishesDO;
import com.yunpeng.dishes.dao.model.entity.MerchantDishesDO;
import com.yunpeng.dishes.dao.model.relation.DishesFoodIngredientsDO;
import com.yunpeng.dishes.dao.model.relation.DishesNutrientsDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * DishesInternalServiceImpl
 *
 * @author : Moore(hemw)
 * @date : 2020-03-27
 */
@Service
public class DishesInternalServiceImpl implements IDishesInternalService {

    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(DishesInternalServiceImpl.class);

    @Resource
    private IMerchantDishesMapper merchantDishesMapper;

    @Resource
    private IDishesMapper dishesMapper;

    @Resource
    private IDishesNutrientsMapper dishesNutrientsMapper;

    @Resource
    private IDishesFoodIngredientsMapper dishesFoodIngredientsMapper;

    /**
     * 菜品保存服务
     *
     * @param dishesDO
     * @param dishesFoodIngredientsDOList
     * @param dishesNutrientsDOList
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 3600, rollbackFor = Exception.class)
    public void saveMerchantDishes(MerchantDishesDO dishesDO, List<DishesFoodIngredientsDO> dishesFoodIngredientsDOList, List<DishesNutrientsDO> dishesNutrientsDOList) {
        LOGGER.info("transaction function start,main parameter: {}", dishesDO);
        if (dishesDO.getId() == null) {
            merchantDishesMapper.add(dishesDO);
        } else {
            merchantDishesMapper.update(dishesDO.getId(), dishesDO.toMap());
        }
        if (dishesFoodIngredientsDOList != null && !dishesFoodIngredientsDOList.isEmpty()) {
            dishesFoodIngredientsMapper.deleteByDishesCode(dishesDO.getCode());
            dishesFoodIngredientsMapper.batchAdd(dishesFoodIngredientsDOList);
        }
        if (dishesNutrientsDOList != null && !dishesNutrientsDOList.isEmpty()) {
            dishesNutrientsMapper.deleteByDishesCode(dishesDO.getCode());
            dishesNutrientsMapper.batchAdd(dishesNutrientsDOList);
        }
        LOGGER.info("transaction function end,main parameter: {}", dishesDO);
    }

    @Override
    /**
     * 复制为菜品模版
     * @param dishesDO
     * @param dishesFoodIngredientsDOList
     * @param dishesNutrientsDOList
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 3600, rollbackFor = Exception.class)
    public void copyToDishesTemplate(DishesDO dishesDO, List<DishesFoodIngredientsDO> dishesFoodIngredientsDOList, List<DishesNutrientsDO> dishesNutrientsDOList) {
        LOGGER.info("transaction function start,main parameter: {}", dishesDO);
        dishesMapper.add(dishesDO);
        if (dishesFoodIngredientsDOList != null && !dishesFoodIngredientsDOList.isEmpty()) {
            dishesFoodIngredientsMapper.batchAdd(dishesFoodIngredientsDOList);
        }
        if (dishesNutrientsDOList != null && !dishesNutrientsDOList.isEmpty()) {
            dishesNutrientsMapper.batchAdd(dishesNutrientsDOList);
        }
        LOGGER.info("transaction function end,main parameter: {}", dishesDO);
    }

}
